/********************************************************************************
  * @file     SD93F115B_spi.h
  * @author   TEST TEAM 												    	
  * @version  v0															    	
  * @date     Septemper 2022												    	
  * @brief
  *
  * Copyright (C) 2022  Hangzhou SDIC Microelectronics Co., Ltd
**********************************************************************************/


/* Define to prevent recursive inclusion */
#ifndef __SD93F115B_SPI_H
#define __SD93F115B_SPI_H

#ifdef __cplusplus
 extern "C" {
#endif

/********Includes***********/
#include "SD93F115B.h"  

/*********************** SPI_mode *******************************/
#define SPI_Mode_Master                   SPI_CR_MSTR
#define SPI_Mode_Slave                    ((uint32_t)0x00000000)

#define IS_SPI_Mode(Mode) (((Mode) == SPI_Mode_Master)||((Mode) == SPI_Mode_Slave))

/********************** SPI_Clock_Polarity **********************/
#define SPI_CPOL_Low                      ((uint32_t)0x00000000)
#define SPI_CPOL_High                     SPI_CR_CPOL

#define IS_SPI_CPOL(CPOL) (((CPOL) == SPI_CPOL_Low)||((CPOL) == SPI_CPOL_High))

/************************ SPI_Clock_Phase ************************/
#define SPI_CPHA_1Edge                    ((uint16_t)0x0000)
#define SPI_CPHA_2Edge                    SPI_CR_CPHA

#define IS_SPI_CPHA(CPHA) (((CPHA) == SPI_CPHA_1Edge)||((CPHA) == SPI_CPHA_2Edge))

/****************** SPI_MSB_LSB_transmission *********************/
#define SPI_FirstBit_MSB                 ((uint32_t)0x00000000)
#define SPI_FirstBit_LSB                 SPI_CR_LSBFIRST

#define IS_SPI_FIRST_BIT(BIT) (((BIT) == SPI_FirstBit_MSB)||((BIT) == SPI_FirstBit_LSB))	
	
/*****************SPI_Slave_Select_management********************/
#define SPI_NSS_Soft                     SPI_CR_SSINM
#define SPI_NSS_Hard                     ((uint32_t)0x00000000)

#define IS_SPI_NSS(NSS) (((NSS) == SPI_NSS_Soft) || ((NSS) == SPI_NSS_Hard))
	
/*********************** SPI_data_direction ********************/  
#define SPI_Direction_2Lines_FullDuplex ((uint16_t)0x0000)
#define SPI_Direction_2Lines_RxOnly     ((uint16_t)0x0400)
#define SPI_Direction_1Line_Rx          ((uint16_t)0x8000)
#define SPI_Direction_1Line_Tx          ((uint16_t)0xC000)
#define IS_SPI_DIRECTION_MODE(MODE) (((MODE) == SPI_Direction_2Lines_FullDuplex)||((MODE) == SPI_Direction_2Lines_RxOnly)||\
                                     ((MODE) == SPI_Direction_1Line_Rx)||((MODE) == SPI_Direction_1Line_Tx))
									 
									 
/**********************SPI_data_size********************/              
#define SPI_DataSize_4b                 ((uint8_t)0x04)
#define SPI_DataSize_5b                 ((uint8_t)0x05)
#define SPI_DataSize_6b                 ((uint8_t)0x06)
#define SPI_DataSize_7b                 ((uint8_t)0x07)
#define SPI_DataSize_8b                 ((uint8_t)0x08)
#define SPI_DataSize_9b                 ((uint8_t)0x09)
#define SPI_DataSize_10b                ((uint8_t)0x0A)
#define SPI_DataSize_11b                ((uint8_t)0x0B)
#define SPI_DataSize_12b                ((uint8_t)0x0C)
#define SPI_DataSize_13b                ((uint8_t)0x0D)
#define SPI_DataSize_14b                ((uint8_t)0x0E)
#define SPI_DataSize_15b                ((uint8_t)0x0F)
#define SPI_DataSize_16b                ((uint8_t)0x10)

#define SPI_DataSize_17b                ((uint8_t)0x11)
#define SPI_DataSize_18b                ((uint8_t)0x12)
#define SPI_DataSize_19b                ((uint8_t)0x13)
#define SPI_DataSize_20b                ((uint8_t)0x14)
#define SPI_DataSize_21b                ((uint8_t)0x15)
#define SPI_DataSize_22b                ((uint8_t)0x16)
#define SPI_DataSize_23b                ((uint8_t)0x17)
#define SPI_DataSize_24b                ((uint8_t)0x18) 
#define SPI_DataSize_25b                ((uint8_t)0x19)
#define SPI_DataSize_26b                ((uint8_t)0x1A)
#define SPI_DataSize_27b                ((uint8_t)0x1B)
#define SPI_DataSize_28b                ((uint8_t)0x1C)
#define SPI_DataSize_29b                ((uint8_t)0x1D)
#define SPI_DataSize_30b                ((uint8_t)0x1E)
#define SPI_DataSize_31b                ((uint8_t)0x1F)
#define SPI_DataSize_32b                ((uint8_t)0x03)
#define SPI_DataSize_8b_00              ((uint8_t)0x00)
#define SPI_DataSize_16b_01             ((uint8_t)0x01)
#define SPI_DataSize_24b_02             ((uint8_t)0x02)  

#define IS_SPI_Data_Size(Size) (((Size) == SPI_DataSize_4b) || ((Size) == SPI_DataSize_5b) || \
								((Size) == SPI_DataSize_6b) || ((Size) == SPI_DataSize_7b) || \
								((Size) == SPI_DataSize_8b) || ((Size) == SPI_DataSize_9b) || \
								((Size) == SPI_DataSize_10b)|| ((Size) == SPI_DataSize_11b)|| \
								((Size) == SPI_DataSize_12b)|| ((Size) == SPI_DataSize_13b)|| \
								((Size) == SPI_DataSize_14b)|| ((Size) == SPI_DataSize_15b)|| \
								((Size) == SPI_DataSize_16b)|| ((Size) == SPI_DataSize_17b)|| \
								((Size) == SPI_DataSize_18b)|| ((Size) == SPI_DataSize_19b)|| \
								((Size) == SPI_DataSize_20b)|| ((Size) == SPI_DataSize_21b)|| \
								((Size) == SPI_DataSize_22b)|| ((Size) == SPI_DataSize_23b)|| \
								((Size) == SPI_DataSize_24b)|| ((Size) == SPI_DataSize_25b)|| \
								((Size) == SPI_DataSize_26b)|| ((Size) == SPI_DataSize_27b)|| \
								((Size) == SPI_DataSize_28b)|| ((Size) == SPI_DataSize_29b)|| \
								((Size) == SPI_DataSize_30b)|| ((Size) == SPI_DataSize_31b)|| \
								((Size) == SPI_DataSize_8b_00)|| ((Size) == SPI_DataSize_16b_01)||\
								((Size) == SPI_DataSize_24b_02))


/******************SPI_BaudRate_Prescaler **********************/
#define SPI_BaudRatePrescaler_2          ((uint8_t)0x00)
#define SPI_BaudRatePrescaler_4          ((uint8_t)0x01)
#define SPI_BaudRatePrescaler_8          ((uint8_t)0x02)
#define SPI_BaudRatePrescaler_16         ((uint8_t)0x03)
#define SPI_BaudRatePrescaler_32         ((uint8_t)0x04)
#define SPI_BaudRatePrescaler_64         ((uint8_t)0x05)
#define SPI_BaudRatePrescaler_128        ((uint8_t)0x06)
#define SPI_BaudRatePrescaler_256        ((uint8_t)0x07)

#define SPI_BaudRatePrescaler_512        ((uint8_t)0x08)
#define SPI_BaudRatePrescaler_1024       ((uint8_t)0x09)
#define SPI_BaudRatePrescaler_2048       ((uint8_t)0x0A)

#define IS_SPI_BaudRate_Prescaler(Prescaler) (((Prescaler) == SPI_BaudRatePrescaler_2) ||\
                                              ((Prescaler) == SPI_BaudRatePrescaler_4) ||\
                                              ((Prescaler) == SPI_BaudRatePrescaler_8) ||\
                                              ((Prescaler) == SPI_BaudRatePrescaler_16)||\
                                              ((Prescaler) == SPI_BaudRatePrescaler_32)||\
                                              ((Prescaler) == SPI_BaudRatePrescaler_64)||\
                                              ((Prescaler) == SPI_BaudRatePrescaler_128) ||\
                                              ((Prescaler) == SPI_BaudRatePrescaler_256) ||\
                                              ((Prescaler) == SPI_BaudRatePrescaler_512) ||\
                                              ((Prescaler) == SPI_BaudRatePrescaler_1024)||\
                                              ((Prescaler) == SPI_BaudRatePrescaler_2048))
						

/********** SPI_I2S_interrupts_definition ************************/
#define SPI_IT_RXNEIE                  	 SPI_CR_RXNEIE
#define SPI_IT_TXEIE                   	 SPI_CR_TXEIE
#define SPI_IT_OVRIE                   	 SPI_CR_OVRIE
#define SPI_IT_UDRIE                  	 SPI_CR_UDRIE
#define SPI_IT_MODFIE                  	 SPI_CR_MODFIE

#define IS_SPI_ITSource(IT) (((IT) == SPI_IT_RXNEIE)||((IT) == SPI_IT_TXEIE)||\
							 ((IT) == SPI_IT_OVRIE) ||((IT) == SPI_IT_UDRIE)||\
							 ((IT) == SPI_IT_MODFIE))

/************** SPI_I2S_interrupts_definition *******************/
#define SPI_Flag_RXNE                  ((uint8_t)0x01)
#define SPI_Flag_TXE                   ((uint8_t)0x02)
#define SPI_Flag_OVR                   ((uint8_t)0x04)
#define SPI_Flag_UDR                   ((uint8_t)0x08)
#define SPI_Flag_MODF                  ((uint8_t)0x10)
#define SPI_Flag_BSY                   ((uint8_t)0x20)

#define IS_SPI_StatusFlag(Flag) (((Flag) == SPI_Flag_RXNE)|| ((Flag) == SPI_Flag_TXE)||\
								 ((Flag) == SPI_Flag_OVR) || ((Flag) == SPI_Flag_UDR)||\
								 ((Flag) == SPI_Flag_MODF)|| ((Flag) == SPI_Flag_BSY))

#define IS_SPI_ITFlag(Flag)     (((Flag) == SPI_Flag_RXNE)|| ((Flag) == SPI_Flag_TXE)||\
								 ((Flag) == SPI_Flag_OVR) || ((Flag) == SPI_Flag_UDR)||\
								 ((Flag) == SPI_Flag_MODF))

							
#define IS_SPI_ClearFlag(Flag)  (((Flag) == SPI_Flag_OVR)||((Flag) == SPI_Flag_UDR)||\
								 ((Flag) == SPI_Flag_MODF))

/***************SPI Init structure definition**************/
typedef struct
{
  uint32_t SPI_Mode;                             //设置SPI主从模式      
   
  uint32_t SPI_DataSize;                         //配置传输数据字长          
   
  uint32_t SPI_CPOL;                             //配置时钟极性              
   
  uint32_t SPI_CPHA;                             //配置时钟相位                
   
  uint32_t SPI_NSS;                              //配置软件从设备管理                
   
  uint32_t SPI_BaudRatePrescaler;                //配置通信波特率 
   
  uint32_t SPI_FirstBit;                         //配置通信帧格式           
  
}SPI_InitTypeDef;
	
	
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
/* Initialization and Configuration functions *********************************/

void SPI_DeInit(void);                                                  //将SPI相关寄存器恢复为上电默认值 
void SPI_Init(SPI_InitTypeDef* SPI_InitStruct);                         //初始化SPI
void SPI_Cmd(FunctionalState NewState);                                 //使能SPI功能

void SPI_NSSInternalSoftwareCmd(FunctionalState NewState);              //使能内部NSS信号输出
void SPI_NSSOutputCmd(FunctionalState NewState);					    //使能NSS引脚输出
void SPI_NSSPulseModeCmd(FunctionalState NewState);						//使能NSS的脉冲管理
	
/* Data transfers functions ***************************************************/
void SPI_SendData(uint32_t Data);                                       //发送数据
uint32_t SPI_ReceiveData(void);                                         //接收数据

/* Interrupts and flags management functions **********************************/
void SPI_ITConfig(uint32_t SPI_IT, FunctionalState NewState);           //使能SPI相应中断功能
ITStatus SPI_GetITStatus(uint8_t SPI_Flag);                             //获取SPI相应中断标志位
FlagStatus SPI_GetFlagStatus(uint8_t SPI_Flag);                         //获取SPI相应状态标志位
void SPI_ClearFlag(uint8_t SPI_Flag);                                   //清除SPI相应标志位


#ifdef __cplusplus
}
#endif

#endif /* __SD93F115B_SPI_H */


/**********************************END OF FILE**********************************/